import React, { Component } from 'react'
import { BrowserRouter as Routers,  Route, Switch} from 'react-router-dom'
import cookie from 'js-cookie'
import routes from './router/route.js'
import { _checkLogin } from './apis'

const token = cookie.get('token')
class App extends Component {
    constructor(props) {
        super(props)
        this.state = {
            loginStatus:  token !== '' && token !== undefined ? true : false
        }
    }


    async componentDidMount() {
        if (this.state.loginStatus) {
            const data = await _checkLogin({token: token})
            if (data && data.status === 'success' && data.result) {
                if (data.result.hasOwnProperty('loginStatus') && data.result.loginStatus !== 'on') {
                    this.setState({loginStatus: false})
                } else {
                    this.setState({loginStatus: true})
                }
            } else {
                this.setState({loginStatus: false})
            }
        }
    }
  render() {
    return (
      <Routers basename={`/home`}>
        <Switch >
          {
            routes.map(({ name, path, exact = true, component }) => (
              <Route path = {path} exact={exact} component={component} key={name} />
            ))
          }
        </Switch>
      </Routers>
    );
  }
}

export default App;
